<?php
/**
 * Class UserModuleChild
 * 数据表 id username email nickname wechat_id code enable create_time last_time(最后一次登录时间) type
 */
class UserChildModule extends UserModule{
    protected $table;
    protected static $selfObjectArray;
    protected function __construct($table) {
        parent::__construct($table);
        $this->table=!empty($table)&&validateVar($table)?$table:'likyh_user';
        SimpleSession::init();
    }

    /**
     * 初始化函数，并返回当前类
     * @param string $table 表名
     * @return UserChildModule
     */
    public static function init($table = 'likyh_user'){
        if(!isset(self::$selfObjectArray[$table])){
            self::$selfObjectArray[$table]=new UserChildModule($table);
        }
        return self::$selfObjectArray[$table];
    }
    function getUserDetail($ids){
        if(empty($ids)) return null;
        $sql="SELECT `id`,`username`,`email`,`nickname`,`wechat_id`,`enable`,`create_time`,
        `last_time`,`type`,`province_id`,`city_id`,`district_id`,`detailaddress` FROM `{$this->table}` where ".$this->db->getIdCondition($ids,'id');
        $r=$this->db->getAll($sql);
        if($r){
          return is_array($ids) ? $r:$r[0];  
        }    
    }
    //修改资料 2015-08-31
    function modifyInfo_new($id,$data){
        $id=(int)$id;
        return $this->db->update($this->table,$id,$data) == 1;
    }
    
    //根据搜索条件查询用户信息
    function getQuerydetail($username,$realname,$province,$city,$district,$rows=30,$offset=0){
        $condition="";
        if($username){
            $username=$this->db->quote("%{$username}%");
            $condition.=" AND a.username like $username ";
        }
        if($realname){
            $realname=$this->db->quote("%{$realname}%");
            $condition.=" AND b.real_name like $realname ";
        }
        if($province){
            $condition.=" AND a.province_id='$province' "; 
        }
        if($city){
            $condition.=" AND a.city_id='$city' "; 
        }
        if($district){
            $condition.=" AND a.district_id='$district' "; 
        }
        $sql="SELECT SQL_CALC_FOUND_ROWS a.`id`,`username`,`email`,`nickname`,`wechat_id`,`enable`,`create_time`,
        `last_time`,`type`,`province_id`,`city_id`,`district_id`,`detailaddress` FROM user a left join user_info b on a.id=b.user_id where 1".$condition." order by create_time desc limit $offset,$rows";
        $r=$this->db->getList($sql);
       
        return $r;
    }
   
} 